package com.zhiyou100.loans.service.impl;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.zhiyou100.loans.dao.LoanbillDao;
import com.zhiyou100.loans.dao.impl.LoanbillDaoImpl;
import com.zhiyou100.loans.model.Loanbill;
import com.zhiyou100.loans.model.Result;
import com.zhiyou100.loans.model.User;
import com.zhiyou100.loans.service.LoanbillService;
import com.zhiyou100.loans.util.JsonDateProcessor;
import com.zhiyou100.loans.util.NetDate;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class LoanbillServiceImpl implements LoanbillService {
	LoanbillDao ld = new LoanbillDaoImpl();
	@Override
	public JSONObject add(Loanbill bill, HttpServletRequest request) {
		//获取网络时间
		long time=0;
		try {
			URL url = new URL("http://www.baidu.com");
			URLConnection conn=url.openConnection();
			time=conn.getDate();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Date date = new Date(time);
		
		Calendar cal = Calendar.getInstance();//当前的日历类对象
		//起始时间
		bill.setStarttime(date);
		//截止时间的日期  08:00:00
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, +7);
		//设置小时和分钟、以及秒
		cal.set(Calendar.HOUR_OF_DAY, 8);
		cal.set(Calendar.MINUTE,0);
		cal.set(Calendar.SECOND, 0);
		bill.setEndtime(cal.getTime());
		//信息状态
		bill.setMessagestate(1);//1，有效。0，无效
		//借款人id
		User user = (User)(request.getSession().getAttribute("user"));
		bill.setUserid(user.getId());
		//成功状态
		bill.setSuccessstate(0);//1，成功。0，失败
		//信用等级
		bill.setCreditid(1);
		
		int i=ld.add(bill);
		Result result = new Result(false, "借款请求失败");
		if(i>0){
			result.setFlag(true);
			result.setMessage("借款请求成功");
		}
		return JSONObject.fromObject(result);
	}
	@Override
	public JSONArray selectLoanbillInfo() {
		ResultSet rs = ld.selectLoanbillInfo();
		List<Loanbill> list = new ArrayList<>();
		try {
			while(rs.next()){
				Loanbill lb = new Loanbill();
				//消费类型、借款金额、时长、起始时间、截止时间，月利息、信誉等级、输入你的借款金额、操作.隐藏借款人信息
				lb.setUserid(rs.getInt("userid"));//借款人id
				lb.setStarttime(rs.getTimestamp("starttime"));//信息的起始时间
				lb.setEndtime(rs.getTimestamp("endtime"));//信息的截止时间
				lb.setMoney(rs.getDouble("money"));//借款金额
				lb.setDuration(rs.getInt("duration"));//借款时长
				lb.setMonthrate(rs.getDouble("monthrate"));//月利息
				lb.setName(rs.getString("name"));//消费类型
				lb.setCreditname(rs.getString("grade"));//信用等级
				lb.setId(rs.getInt("id"));
				lb.setLendmoney(rs.getDouble("lendmoney"));
				list.add(lb);
				System.out.println("starttime:"+rs.getTimestamp("starttime"));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//json配置
		JsonConfig config = new JsonConfig();
		//使用自己的json日期处理器
		config.registerJsonValueProcessor(Date.class, new JsonDateProcessor());
		return JSONArray.fromObject(list,config);
	}
	@Override
	public JSONObject selectByUserid(String id) {
		ResultSet rs =ld.selectByUserid(Integer.parseInt(id));
		Result result = new Result(true, "");
		try {
			if(rs.next()){
				result.setFlag(false);
				result.setMessage("正在查询个人借款数据.....");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return JSONObject.fromObject(result);
	}
	@Override
	public JSONObject selectlbip(String id) {
		ResultSet rs = ld.selectlbip(Integer.parseInt(id));
		Loanbill lb=null;
		try {
			if(rs.next()){
				lb=new Loanbill();
				lb.setUserid(rs.getInt("userid"));//借款人id
				lb.setStarttime(rs.getTimestamp("starttime"));//信息的起始时间
				lb.setEndtime(rs.getTimestamp("endtime"));//信息的截止时间
				lb.setMoney(rs.getDouble("money"));//借款金额
				lb.setDuration(rs.getInt("duration"));//借款时长
				lb.setMonthrate(rs.getDouble("monthrate"));//月利息
				lb.setName(rs.getString("name"));//消费类型
				lb.setCreditname(rs.getString("grade"));//信用等级
				lb.setSuccendtime(rs.getTimestamp("succendtime"));//成功结束时间
				lb.setSuccstarttime(rs.getTimestamp("succstarttime"));//成功开始时间
				lb.setSuccessstate(rs.getInt("successstate"));//借款状态
				lb.setId(rs.getInt("id"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JsonConfig config = new JsonConfig();
		config.registerJsonValueProcessor(Date.class, new JsonDateProcessor());
		return JSONObject.fromObject(lb,config);
	}
	@Override
	public JSONObject update(String id) {
		//需要当前的时间
		Date current = NetDate.getCurrentTime();
		Loanbill lb = new Loanbill();
		lb.setId(Integer.parseInt(id));
		lb.setSuccstarttime(current);
		lb.setSuccessstate(1);//借款成功状态
		lb.setMessagestate(0);//借款信息失效状态
		
		//最终还款时间，每月10号，必须获得借款时长
		int months = getMonthByLbid(id);//借款时长
		Calendar cal = Calendar.getInstance();
		cal.setTime(current);
		cal.add(Calendar.MONTH, months);
		cal.set(Calendar.DAY_OF_MONTH, 10);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		
		Date end = cal.getTime();
		lb.setSuccendtime(end);
		
		int i = ld.update(lb);
		Result result = new Result(false, "借款单状态修改失败");
		if(i>0){
			result.setFlag(true);
			result.setMessage("借款单状态修改成功");
		}
		return JSONObject.fromObject(result);
	}
	
	
	public int getMonthByLbid(String id){
		ResultSet rs = ld.selectMonthByLbid(Integer.parseInt(id));
		int month=0;
		try {
			if(rs.next()){
				month = rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return month;
	}

}
